<?php
/**
 * Created by PhpStorm. 20150326
 * User: sunqiang
 * Date: 15-3-26
 * Time: 下午5:24
 */


class taskLogHandler {
    private $dbca;

    function __construct( &$dbca ) {
        $this->dbca = $dbca;
    }

    public function saveLogInfo( $operator , $name ,  $message='' ,  $level = 0 ) {

        if (!isset($name) || empty($name)){
            return array('code' => 400, 'message' => '任务名称为空');
        }

        try {
            //保存日志信息
            $log = new entity('log_task');
            $log->creator = $operator;
            $log->level = $level;
            $log->name = $name;
            $log->message = $message;
            $this->dbca->save($log);


            if( $level == 9 )
            {
                $msg = '[致命错误]['. $name .']'.$message;

                $sms1 = new entity('task_sms_sent');
                $sms1->creator = $operator;
                $sms1->sms_code = 'task_log_handler';
                $sms1->mobile = '18516203535';
                $sms1->sms_content = $msg;
                $sms1->sent_status = 0;
                $this->dbca->save($sms1);

                $sms2 = new entity('task_sms_sent');
                $sms2->creator = $operator;
                $sms2->sms_code = 'task_log_handler';
                $sms2->mobile = '13636312925';
                $sms2->sms_content = $msg;
                $sms2->sent_status = 0;
                $this->dbca->save($sms2);

                $sms3 = new entity('task_sms_sent');
                $sms3->creator = $operator;
                $sms3->sms_code = 'task_log_handler';
                $sms3->mobile = '18221723400';
                $sms3->sms_content = $msg;
                $sms3->sent_status = 0;
                $this->dbca->save($sms3);

            }
            return array('code' => 200, 'message' => '日志保存成功');

        } catch (Exception $e) {

            if($e->getCode() > 0){
                return array('code' => $e->getCode(), 'message' => $e->getMessage());
            }else{
                error_log($e->getMessage());
                return array('code' => 500, 'message' => '系统错误');
            }

        }

    }

} 